package com.industrial.white.controller;


import com.industrial.common.core.domain.R;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.system.api.domain.RegisterWhiteUserForm;
import com.industrial.system.api.domain.SysUser;
import com.industrial.white.domain.WhiteIndustrialArea;
import com.industrial.white.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 一个基础功能获取数据的Controller
 * @Date 2023/8/15 10:54
 * @Author SangGuangCan
 */
@RestController
@RequestMapping("/common")
public class ComController {

    @Autowired
    private ICommonService commonService;


    /**
     * 获取
     * @param adCode
     * @return
     */
    @GetMapping("/getAd")
    public AjaxResult getAdministrativeDistrictData(@RequestParam(value = "adCode",defaultValue = "0") String adCode){
        List<WhiteIndustrialArea> result = this.commonService.getAdministrativeDistrictData(adCode);
        return AjaxResult.success(result);
    }

    @GetMapping("/loadAdCodeToAdBaseId/{adCode}")
    public R<String> loadAdCodeToAdBaseId(@PathVariable("adCode") String adCode){
       String adName = this.commonService.loadAdCodeToAdBaseId(adCode);
       return R.ok(adName);
    }

    /**
     * <p>
     *     注册白名单用户
     * </p>
     * @return
     */
    @PostMapping("/registerWithUser")
    public AjaxResult registerWithUser(@RequestBody RegisterWhiteUserForm params){
        AjaxResult result = commonService.registerWithUser(params);
        return result;
    }

    /**
     * 查询行政区代码所对应的行政区ID
     * @param codes
     * @return
     */
    @GetMapping("/queryWhiteAreaId/{codes}")
    public R<Map<String,String>> queryWhiteAreaId(@PathVariable("codes") String codes){
        List<WhiteIndustrialArea> r = commonService.queryWhiteAreaId(codes);

        Map<String,String> rMap = new HashMap<>(16);

        if(r != null && !r.isEmpty()){
            rMap = r.stream().collect(Collectors.toMap(WhiteIndustrialArea::getAreaCode, WhiteIndustrialArea::getBaseId));
        }
        return R.ok(rMap);
    }
}
